package cn.b.sky.mes.hr.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.hr.model.PersonnelTypeAssign;
import cn.b.sky.mes.hr.model.ShiftingPermission;
import cn.b.sky.mes.hr.model.condition.PersonnelTypeAssignCondition;
import cn.b.sky.mes.hr.service.PersonnelTypeAssignService;
import cn.b.sky.mes.hr.service.ShiftingPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.cm.enums.ECodeEnums;
import cn.b.sky.user.service.ECodeService;
import cn.b.sky.user.service.model.ECode;

/**
 * 
 * <strong>Title : PersonnelTypeAssignController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.hr.controller<br></strong>
 * <strong>Description : </strong>人员类型控制器<br> 
 * <strong>Create on : 2016-3-24 下午1:43:24<br></strong>
 * <p>
 * @author lidapeng<br> 
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("ptassign")
@Slf4j
public class PersonnelTypeAssignController extends SecurityController {
  @Resource
  private PersonnelTypeAssignService personnelTypeAssignService;

  @Resource
  private ECodeService eCodeService;

  @Resource
  private ShiftingPermissionService shiftingPermissionService;

  @RequestMapping("personnelTypeAssignList")
  public String personnelTypeAssignList(@ModelAttribute("condition") PersonnelTypeAssignCondition condition,
      String navTabId, Model model) {
    condition.setSiteCode(this.getSiteCode());
    //获取当前用户的工作中心权限
    List<ShiftingPermission> workCenters = shiftingPermissionService.getPermissionsByUser(this.getSiteCode(),
        SecurityContextUtil.getCurrentUserName(), null);
    //没有工作中心权限则不过滤
    if (workCenters == null || workCenters.size() <= 0) {
      condition.setData(new ArrayList<PersonnelTypeAssign>());
      condition.setTotalCount(0);
    } else {
      condition.setPermission(workCenters);
      List<PersonnelTypeAssign> personnelTypeAssigns = personnelTypeAssignService
          .getPersonnelTypeAssignsByCondition(condition);
      int count = personnelTypeAssignService.getPersonnelTypeAssignCountByCondition(condition);
      condition.setData(personnelTypeAssigns);
      condition.setTotalCount(count);
    }
    model.addAttribute("page", condition);
    List<ECode> ecodes = eCodeService.selectECodeList(ECodeEnums.PERSONNELSKILLLEVELE.getCode(),
        this.getCurrentLanguage());
    model.addAttribute("killLevele", ecodes);
    model.addAttribute(NAVTABID, navTabId);
    return "hr/personnelTypeAssign/personnelTypeAssign_list";
  }

  @RequestMapping("addPersonnelTypeAssign")
  public String addPersonnelTypeAssign(String navTabId, Model model) {

    List<ECode> ecodes = eCodeService.selectECodeList(ECodeEnums.PERSONNELSKILLLEVELE.getCode(),
        this.getCurrentLanguage());
    model.addAttribute("killLevele", ecodes);
    model.addAttribute(NAVTABID, navTabId);
    return "hr/personnelTypeAssign/personnelTypeAssign_add";
  }

  @RequestMapping("modifyPersonnelTypeAssign")
  public String modifyPersonnelTypeAssign(String id, String navTabId, Model model) {

    if (StringUtils.isBlank(id) || id.indexOf(SystemConstant.MIDDLE_SPLIT) == -1) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    String[] ids = id.split(SystemConstant.MIDDLE_SPLIT);
    if (ids.length != 2) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    String personnelCode = ids[0];
    String personnelTypeCode = ids[1];
    List<ECode> ecodes = eCodeService.selectECodeList(ECodeEnums.PERSONNELSKILLLEVELE.getCode(),
        this.getCurrentLanguage());
    PersonnelTypeAssign personnelTypeAssign = personnelTypeAssignService.getPersonnelTypeAssignByPk(personnelCode,
        personnelTypeCode, this.getSiteCode());
    model.addAttribute("killLevele", ecodes);
    model.addAttribute("personnelTypeAssign", personnelTypeAssign);
    model.addAttribute(NAVTABID, navTabId);
    return "hr/personnelTypeAssign/personnelTypeAssign_edit";
  }

  @RequestMapping("viewPersonnelTypeAssign")
  public String viewPersonnelTypeAssign(String id, String navTabId, Model model) {
    if (StringUtils.isBlank(id) || id.indexOf(SystemConstant.MIDDLE_SPLIT) == -1) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    String[] ids = id.split(SystemConstant.MIDDLE_SPLIT);
    if (ids.length != 2) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    String personnelCode = ids[0];
    String personnelTypeCode = ids[1];
    PersonnelTypeAssign personnelTypeAssign = personnelTypeAssignService.getPersonnelTypeAssignByPk(personnelCode,
        personnelTypeCode, this.getSiteCode());

    model.addAttribute("personnelTypeAssign", personnelTypeAssign);
    model.addAttribute(NAVTABID, navTabId);
    List<ECode> ecodes = eCodeService.selectECodeList(ECodeEnums.PERSONNELSKILLLEVELE.getCode(),
        this.getCurrentLanguage());
    model.addAttribute("killLevele", ecodes);
    return "hr/personnelTypeAssign/personnelTypeAssign_view";
  }

  @RequestMapping("savePersonnelTypeAssign")
  @ResponseBody
  public DWZResult savePersonnelTypeAssign(PersonnelTypeAssign pta, Model model, String navTabId) {
    if (pta == null || pta.isNullObject()) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    PersonnelTypeAssign alreads = personnelTypeAssignService.getPersonnelTypeAssignByPk(pta.getPersonnelCode(),
        pta.getPersonnelTypeCode(), this.getSiteCode());
    if (alreads != null && !alreads.isNullObject()) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), "personnelTypeAssignAlread.M",
          pta.getPersonnelCode()));
    }
    pta.setSiteCode(this.getSiteCode());
    //this.setCreateInfo(personnelTypeAssign);

    personnelTypeAssignService.savePersonnelTypeAssign(pta);

    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("updatePersonnelTypeAssign")
  @ResponseBody
  public DWZResult updatePersonnelTypeAssign(PersonnelTypeAssign pta, Model model, String navTabId) {
    if (pta == null || pta.isNullObject()) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }

    pta.setSiteCode(this.getSiteCode());
    //this.setCreateInfo(personnelTypeAssign);

    personnelTypeAssignService.modifyPersonnelTypeAssign(pta);

    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("deletePersonnelTypeAssign")
  @ResponseBody
  public DWZResult deletePersonnelTypeAssign(String ids, String navTabId, Model model, HttpServletRequest request) {
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] idArr = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String[] codes = idArr[i].split(SystemConstant.MIDDLE_SPLIT);
      String message = null;
      //如果主键没有下划线，则报错，删除失败，主键由personnelCode_personnelTypeCode组成
      try {
        message = personnelTypeAssignService.deletePersonnelTypeAssignByPk(codes[0], codes[1], this.getSiteCode());
        message = this.getImeViewMessageI18n(this.getCurrentLanguage(), message);
      } catch (Exception e) {
        log.error("{}", e);
        message = e.getMessage();
      }
      boolean flag = true;
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        flag = false;
      }
      BatchJobReport report = new BatchJobReport(idArr[i], message, flag, this.getCurrentLanguage(), this.getSiteCode());
      reports.add(report);
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }
  }

  /**
   * 
   * <strong>Title : getPTAsByPersonnel<br></strong>
   * <strong>Description : </strong>查询人员的人员与类型<br> 
   * <strong>Create on : 2016-3-31 下午7:21:00<br></strong>
   * <p>
   * @param personnelCode
   * @return
   * DWZResult
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("getPTAsByPersonnel")
  @ResponseBody
  public DWZResult getPTAsByPersonnel(String personnelCode) {
    if (StringUtils.isBlank(personnelCode)) {
      return ajaxError(getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), SystemConstant.PARAMETERNULL));
    }
    List<PersonnelTypeAssign> ptas = personnelTypeAssignService.getPersonnelTypeAssignsByPersonnel(getSiteCode(),
        personnelCode);
    return ajaxDone(ptas);
  }
}
